package com.mexico.inloancash.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.appsflyer.AppsFlyerLib;
import com.google.gson.Gson;
import com.mexico.inloancash.MyApplication;
import com.mexico.inloancash.R;
import com.mexico.inloancash.bean.BaseBean;
import com.mexico.inloancash.dialog.IndiaCommonDialog;
import com.mexico.inloancash.http.IndiaRequestApi;
import com.mexico.inloancash.http.IndiaDKFactory;
import com.mexico.inloancash.utils.MyPreferencesManager;
import com.mexico.inloancash.utils.MyToast;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * 借款确认
 */
public class IndiaConfirm_LoanActivity extends BaseActivity {

    @BindView(R.id.logon_back)
    ImageView mIndia_GonBack;

    @BindView(R.id.title)
    TextView title;

    @BindView(R.id.money_edit)
    TextView mIndiaTvMoney;

    @BindView(R.id.loan_day)
    TextView mIndiaTvLoanDay;

    @BindView(R.id.jiekuanshu)
    TextView mIndiaTvJks;

    @BindView(R.id.all_money)
    TextView mIndiaTotal_Money;

    @BindView(R.id.fuwufei)
    TextView mIndiaTvff;

    @BindView(R.id.lixi)
    TextView mIndiaLiXi;

    @BindView(R.id.choose_serve)
    ImageView mIndiaIvChoose;

    @BindView(R.id.service)
    TextView mIndiaService;

    @BindView(R.id.tvLoanDetails)
    TextView mIndiaTvLoanDetailsInfo;

    @BindView(R.id.jieqian_bottom)
    Button mIndiaBtnJqBottom;

    @BindView(R.id.ll_sure_layout)
    LinearLayout mIndiallSureLayout;


    private IndiaCommonDialog mIndiaCommonDialog;
    private String orderNo;
    private boolean isChoose = true;
    private String mIndiaDay, mIndiaDaozhang, mIndiaFuwu, mIndiaAllMount, mIndiaLixi1, mIndiaZongshu;//分别代表多少天，到账多少，服务费，还多少，利息多少，总额度多少

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.india_activity_confirm_view_loan_layout);
        ButterKnife.bind(this);
        title.setText("Loan confirmation");
        orderNo = getIntent().getStringExtra("order");
        mIndiaDay = getIntent().getStringExtra("day");
        mIndiaDaozhang = getIntent().getStringExtra("daozhang");
        mIndiaFuwu = getIntent().getStringExtra("fuwu");
        mIndiaAllMount = getIntent().getStringExtra("allMount");
        mIndiaLixi1 = getIntent().getStringExtra("lixi");
        mIndiaZongshu = getIntent().getStringExtra("zongshu");

        //贷款金额
        mIndiaTvJks.setText(mIndiaAllMount);
        mIndiaTvMoney.setText(mIndiaAllMount);//贷款金额
        mIndiaTvLoanDetailsInfo.setText(mIndiaZongshu);//还款金额

        //到账金额
        String damount = getIntent().getStringExtra("damount");
        mIndiaTotal_Money.setText("₹ " + damount);

        //应还款金额
        String payAmount = getIntent().getStringExtra("payAmount");
        mIndiaLiXi.setText("₹ " + payAmount);

        //贷款天数
        mIndiaTvff.setText(mIndiaDay + " days");
        mIndiaTvLoanDay.setText(mIndiaDay + " days");

        mIndiaBtnJqBottom.setSelected(true);

    }

    public void mIndiaComfirm_LoanMoney() {
        loading.show();
        IndiaRequestApi api = IndiaDKFactory.getInstance().getApi();
        Call<String> sureLoan = api.sureLoan(MyPreferencesManager.getString("token", ""), MyApplication.source, orderNo);
        sureLoan.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                if (response.isSuccessful()) {
                    BaseBean baseBean = new Gson().fromJson(response.body(), BaseBean.class);
                    if (baseBean.getCode() == 200) {
                        Map<String, Object> eventValues1 = new HashMap<>();
                        eventValues1.put("sureLoan", "sureLoan");
                        AppsFlyerLib.getInstance().trackEvent(IndiaConfirm_LoanActivity.this, "sureLoan", eventValues1);
                        MyToast.show(IndiaConfirm_LoanActivity.this, "Aplikasi yang sukses");
                        finish();
                    } else if (baseBean.getCode() == 401) {
                        MainActivity.isShow=false;
                        Intent intent = new Intent(IndiaConfirm_LoanActivity.this, IndiaLoginActivity.class);
                        startActivity(intent);
                    } else {
                        MyToast.show(IndiaConfirm_LoanActivity.this, baseBean.getMsg());
                    }
                }
                loading.dismiss();
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                loading.dismiss();
            }
        });
    }

    @OnClick({R.id.logon_back, R.id.service, R.id.jieqian_bottom, R.id.choose_serve, R.id.tv_cancel_order})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.logon_back:
                finish();
                break;
            case R.id.service:
                Intent service = new Intent(this, IndiaWeb_ViewActivity.class);
                startActivity(service);
                break;
            case R.id.jieqian_bottom:
                if (isChoose) {
                    finish();
                    //sureLoan();
                } else {
                    MyToast.show(this, "Please read and agree to the terms of service");
                }
                break;
            case R.id.choose_serve:
                if (isChoose) {
                    mIndiaIvChoose.setImageResource(R.mipmap.india_gocash_picture_land_choice);
                    isChoose = false;
                    mIndiaBtnJqBottom.setSelected(false);
                } else {
                    mIndiaIvChoose.setImageResource(R.mipmap.india_gocash_picture_land_choice_ed);
                    isChoose = true;
                    mIndiaBtnJqBottom.setSelected(true);
                }
                break;
            case R.id.tv_cancel_order:
                //取消订单
                mIndiaCommonDialog = new IndiaCommonDialog(this);
                mIndiaCommonDialog.show();
                mIndiaCommonDialog.findViewById(R.id.btn_ok).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        mIndiaCommonDialog.dismiss();
                        mIndiaCancel_Money_Loan();
                    }
                });
                break;
        }
    }

    /**
     * 取消订单
     */
    public void mIndiaCancel_Money_Loan() {
        loading.show();
        IndiaRequestApi api = IndiaDKFactory.getInstance().getApi();
        Call<String> sureLoan = api.cancelLoan(MyPreferencesManager.getString("token", ""), MyApplication.source, orderNo);
        sureLoan.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                if (response.isSuccessful()) {
                    BaseBean baseBean = new Gson().fromJson(response.body(), BaseBean.class);
                    if (baseBean.getCode() == 200) {
                        MyToast.show(IndiaConfirm_LoanActivity.this, "Successful cancellation");
                        finish();
                    } else if (baseBean.getCode() == 401) {
                        MyPreferencesManager.putString("token", "");
                        MyPreferencesManager.putLogin(false);
                        MainActivity.isShow=false;
                        Intent intent = new Intent(IndiaConfirm_LoanActivity.this, IndiaLoginActivity.class);
                        startActivity(intent);
                    } else {
                        MyToast.show(IndiaConfirm_LoanActivity.this, baseBean.getMsg());
                    }
                }
                loading.dismiss();
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {

                loading.dismiss();
            }
        });
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            finish();
        }
        return super.onKeyDown(keyCode, event);
    }
}
